package com.it.product.comtroller;

import com.it.product.domain.CategoryBrand;
import com.it.product.service.ICategoryBrandService;

import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.ServletException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * projectName: spzx-parent
 *
 * @author: 王宝贵
 * time: 2024/9/20 20:02 周五
 * description:
 */
@Tag(name = "分类品牌")
@RestController
@RequestMapping("/categoryBrand")
public class CategoryBrandController extends BaseController {

    @Autowired
    ICategoryBrandService categoryBrandService;

    /**
     * 分页查询分类品牌
     * @param categoryBrand 查询非条件
     * @return
     */
    @Operation(summary = "分页查询分类品牌")
    @GetMapping("list")
    public TableDataInfo list(CategoryBrand categoryBrand){
        //若伊的分页
        startPage();
        List<CategoryBrand> categoryBrandList = categoryBrandService.PageList(categoryBrand);
        return getDataTable(categoryBrandList);

    }

    // TODO -id回显加递归 新增功能的信息回显
    /**
     * 获取分类信息的详细信息
     * @param id cb的主键id
     * @return
     * 先传入CB的主键 根据主键查询到c的主键id
     * 然后根据主键id查询数据获取父类的id 递归 依次将主键id放到集合里面
     */
    @Operation(summary = "获取分类信息的详细信息")
    @GetMapping("/{id}")
    public AjaxResult selectByCbId(@PathVariable Long id){
        CategoryBrand categoryBrandList= categoryBrandService.selectById(id);
        return success(categoryBrandList);
    }


    /**
     * 新增分类品牌
     * @param categoryBrand
     * @return
     */
    @Operation(summary = "新增分类品牌")
    @PostMapping
    public AjaxResult save(@Validated @RequestBody CategoryBrand categoryBrand) throws ServletException {
        //获取操作人
        categoryBrand.setCreateBy(SecurityUtils.getUsername());
        int rows = categoryBrandService.insertCategoryBrand(categoryBrand);
        return toAjax(rows);
    }


    /**
     * 修改分类品牌
     * @param categoryBrand
     * @return
     */
    @Operation(summary = "修改分类品牌")
    @PutMapping
    public AjaxResult update(@Validated @RequestBody CategoryBrand categoryBrand) throws ServletException {
        //获取操作人
        categoryBrand.setCreateBy(SecurityUtils.getUsername());
        int rows = categoryBrandService.updateCategoryBrand(categoryBrand);
        return toAjax(rows);
    }

    /**
     * 删除分类品牌
     */
    @Operation(summary = "删除分类品牌")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(categoryBrandService.removeBatchByIds(ids));
    }

}